package com.yzz.hzraisedust.hzraseidust.main.airquality;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yzz.hzraisedust.hzraseidust.R;
import com.yzz.hzraisedust.hzraseidust.main.BaseActivity;
import com.yzz.hzraisedust.hzraseidust.modelbean.AirQualityBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.ProjectAirItemBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.RequstItemBean;
import com.yzz.hzraisedust.hzraseidust.util.Constant;
import com.yzz.hzraisedust.hzraseidust.util.JsonUtil;
import com.yzz.hzraisedust.hzraseidust.util.NetUtil;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class AirQualityDetailActivity extends BaseActivity implements Response.Listener<String>,Response.ErrorListener{

    private AirQualityBean mAirQualityBean;

    private RequstItemBean<ProjectAirItemBean> mProjectItem;
    private ListView mListView;
    private TextView mTitleView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_air_quality_detail);
        initLoginingDlg();
        showLoginingDlg();
        initView();
        initData();
    }

    private void initView() {
        TextView emptyTextView = findViewById(R.id.air_quality_empty_list_textview);

        mListView = findViewById(R.id.air_quality_detail_listview);
        mListView.setEmptyView(emptyTextView);
        View include = findViewById(R.id.air_back_include);
        ImageView imageView = include.findViewById(R.id.back_to_pri_imageview);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTitleView = include.findViewById(R.id.title_all_textview);
    }
    private void initData(){
        mAirQualityBean = (AirQualityBean)getIntent().getSerializableExtra(Constant.AIR_QUALITY_BEAN);
        NetUtil netUtil = new NetUtil(AirQualityDetailActivity.this);
        Map<String,String> params = new HashMap<>();
        params.put(Constant.UID_LOWER_KEY,Constant.Uid);
        params.put(Constant.QUALITY_KEY,mAirQualityBean.getQualityName());
        netUtil.requestData(this,this,Constant.AIR_QUALITY_URL,params);
    }

    private void updateListViewsItem(){
        AirQualityAdapter airQualityAdapter = new AirQualityAdapter(AirQualityDetailActivity.this,mProjectItem.getRows());
        mListView.setAdapter(airQualityAdapter);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            finish();
            return  true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        if(volleyError!=null)
            Toast.makeText(AirQualityDetailActivity.this,"出现错误如下:"+volleyError.getLocalizedMessage(),Toast.LENGTH_LONG).show();
        finish();
    }

    @Override
    public void onResponse(String s) {
        Type type = new TypeToken<RequstItemBean<ProjectAirItemBean>>(){}.getType();
        mProjectItem = (RequstItemBean<ProjectAirItemBean>)JsonUtil.jsonToObject(s,type);
        if(mProjectItem!=null && mProjectItem.isSuccess()) {
            updateListViewsItem();
        }else{
            closeLoginingDlg();
            if(mProjectItem!=null)
                Toast.makeText(AirQualityDetailActivity.this,"出现错误如下:"+mProjectItem.getMessage(),Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        closeLoginingDlg();
        Log.i("yjf",s);
    }
}
